<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流程名称" prop="workflowName">
        <el-input v-model="queryParams.workflowName" placeholder="请输入流程名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="statusOpt">
        <el-select v-model="queryParams.statusOpt" placeholder="请选择状态" clearable size="small">
        <el-option v-for="dict in statusOptOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['labdata:flowDefination:edit']">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="flowDefinationList" @selection-change="handleSelectionChange">
      <el-table-column label="编码" prop="workflowId" align="center" width="60"/>
      <el-table-column label="流程名称" prop="workflowName" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <router-link :to="'/flowDefination/node/' + scope.row.workflowId" class="link-type">
            <span>{{ scope.row.workflowName }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="业务类型" prop="businessType" :formatter="businessTypeFormat"  align="center"/>
      <el-table-column label="状态" align="center" prop="statusOpt" :formatter="statusOptFormat" width="60"/>
      <el-table-column label="适用范围"  prop="applyScope" :formatter="applyScopeFormat"  align="center"/>
      <el-table-column label="创建人员" prop="createBy" width="80" align="center"/>
      <el-table-column label="创建时间"  prop="createTime"  align="center">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="160px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handlePreview(scope.row)"
          >查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['labdata:flowDefination:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['labdata:flowDefination:edit']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改流程定义对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="460px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="流程名称" prop="workflowName">
          <el-input v-model="form.workflowName" placeholder="请输入流程名称" class="input_w"/>
        </el-form-item>
        <el-form-item label="业务类型" prop="businessType">
          <el-select v-model="form.businessType" placeholder="业务类型" class="input_w">
            <el-option v-for="dict in businessTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="statusOpt">
          <el-select v-model="form.statusOpt" placeholder="请选择状态" class="input_w">
            <el-option v-for="dict in statusOptOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="适用范围" prop="applyScope">
          <el-select v-model="form.applyScope" placeholder="请选择适用范围" class="input_w">
            <el-option v-for="dict in applyScopeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <FlowPreview  ref="previewCom"/>
  </div>
</template>

<script>
  import {
    listFlowDefination,
    getAllFlowDict,
    getFlowDefination,
    delFlowDefination,
    addFlowDefination,
    updateFlowDefination,
    exportFlowDefination
  } from "@/api/labdata/flowDefination";
  import FlowPreview from "./details.vue";
 /* import {
    treeselect,getTreeLabel
  } from "@/api/labdata/orgnization";

  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css"; */
  export default {
    name: "FlowDefination",
    components: {
      //Treeselect,
      FlowPreview
      },
    data() {
      return {
        // 部门树选项
       // deptOptions: [],
       businessTypeOptions:[],
        //流程选项
        applyScopeOptions:[],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 流程定义表格数据
        flowDefinationList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 状态字典
        statusOptOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          workflowName: null,
          statusOpt: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          workflowName: [{
            required: true,
            message: "流程名称不能为空",
            trigger: "blur"
          }],
          businessType: [{
            required: true,
            message: "业务类型不能为空",
            trigger: "blur"
          }],
          applyScope: [{
            required: true,
            message: "适用范围不能为空",
            trigger: "blur"
          }],
          statusOpt: [{
            required: true,
            message: "状态不能为空",
            trigger: "change"
          }],
        }
      };
    },
    created() {
      this.getList();
      //this.getTreeselect();
      this.getDicts("statusOpt").then(response => {
        this.statusOptOptions = response.data;
      });
      this.getDicts("businessTypeOpt").then(response => {
        this.businessTypeOptions = response.data;
      });
      this.getDicts("applyScopeOpt").then(response => {
        this.applyScopeOptions = response.data;
      });
    },
    methods: {
      handlePreview(row){
        let workflowId = row.workflowId;
        //console.log('workflowId:'+workflowId);
        this.$refs['previewCom'].previewData(workflowId);
      },
      /** 查询流程定义列表 */
      getList() {
        this.loading = true;
        listFlowDefination(this.queryParams).then(response => {
          this.flowDefinationList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 状态字典翻译
      statusOptFormat(row, column) {
        return this.selectDictLabel(this.statusOptOptions, row.statusOpt);
      },
      businessTypeFormat(row, column) {
        return this.selectDictLabel(this.businessTypeOptions, row.businessType);
      },
      applyScopeFormat(row,column){
         return this.selectDictLabel(this.applyScopeOptions,row.applyScope);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          workflowId: null,
          workflowName: null,
          applyScope: null,
          statusOpt: null,
          businessType: null,
          createBy: null,
          createTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.workflowId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加流程定义";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const workflowId = row.workflowId || this.ids
        getFlowDefination(workflowId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改流程定义";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.workflowId != null) {
              updateFlowDefination(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addFlowDefination(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const workflowIds = row.workflowId || this.ids;
        this.$confirm('是否确认删除流程定义编号为"' + workflowIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFlowDefination(workflowIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有流程定义数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportFlowDefination(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
<style type="text/css" lang="scss" scoped>
  .input_w{
    width:260px;
  }
</style>
